/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.org.jhsystem.consola.sedapal;

/**
 *
 * @author usuario
 */
public class Util {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println(codigoBarra("3814342151","36.3"));
        System.out.println(codigoBarra("6130412008","19"));
        System.out.println(codigoBarra("3223080150","4355.3"));
        System.out.println(codigoBarra("3196049150","214.03"));
        System.out.println(codigoBarra("2017649156","10.08"));
        System.out.println(codigoBarra("4218008148","39.47"));
        System.out.println(codigoBarra("3196057180","35.3"));
        System.out.println(codigoBarra("2017499200","108.22"));
        System.out.println(codigoBarra("3196059151","277.22"));
        System.out.println(codigoBarra("4256905148","931.57"));
        System.out.println(codigoBarra("5348623124","94.2"));
        System.out.println(codigoBarra("5356433095","52.19"));
        System.out.println(codigoBarra("4238292148","310.42"));
        System.out.println(codigoBarra("2029068174","238.37"));
        System.out.println(codigoBarra("2029071189","156.62"));
        System.out.println(codigoBarra("2104057154","10.54"));
        System.out.println(codigoBarra("4225505149","17.55"));
        System.out.println(codigoBarra("4239831151","18.01"));
        System.out.println(codigoBarra("3196015192","243.48"));
        System.out.println(codigoBarra("5079193146","26.66"));
        System.out.println(codigoBarra("2017527142","24.71"));
        System.out.println(codigoBarra("4262681148","135.67"));
        System.out.println(codigoBarra("3195958168","138.31"));
        System.out.println(codigoBarra("3195956153","19.54"));
        System.out.println(codigoBarra("3195955150","288.92"));
        System.out.println(codigoBarra("3195953150","78.08"));
        System.out.println(codigoBarra("5622483049","21.5"));
        System.out.println(codigoBarra("3195951160","102.56"));
        System.out.println(codigoBarra("3195948150","83.26"));
        System.out.println(codigoBarra("3195946163","66.72"));
        System.out.println(codigoBarra("3195945150","76.07"));

    }

  public static String codigoBarra(String pmSimbol, String pmImporte) {
    String codBarra = "";

    long ll_numero = 0L;
    String ls_importe = pmImporte;
    String ls_decimal = "";
    int li_decimal = 0;
    int posNum = ls_importe.indexOf(".");
    if(posNum==-1) posNum = ls_importe.indexOf(",");
    if(posNum>-1){
        li_decimal = ls_importe.length() - (posNum + 1);
        ls_importe = ls_importe.replace(".","");
        ls_importe = ls_importe.replace(",","");
    }
    for(int j=li_decimal;j<2;j++) ls_decimal = ls_decimal + "0";
    ls_importe = "000000000000000" + ls_importe + ls_decimal;
    ls_importe = ls_importe.substring(ls_importe.length()-15);

    String ls_vector = pmSimbol;
    int li_largo = ls_vector.length();
    for (int li_punto = 1; li_punto <= li_largo; ++li_punto) {
      String ls_mid = ls_vector.substring(li_punto - 1, li_punto);
      if (ls_mid != null) {
        ll_numero += Long.parseLong(ls_vector.substring(li_punto - 1, li_punto)) * Long.parseLong(String.valueOf(li_punto));
      }
    }
    long dig_aux1 = residuo(ll_numero, 11L);
    if (dig_aux1 == 10L) dig_aux1 = 0L;
    ll_numero = 0L;
    ls_vector = String.valueOf(dig_aux1) + ls_importe;
    li_largo = ls_vector.length();
    for (int li_punto = 1; li_punto <= li_largo; ++li_punto) {
      if ((ls_vector.substring(li_punto - 1, li_punto).equals(" ")) ||
        (ls_vector.substring(li_punto - 1, li_punto).equals(",")) ||
        (ls_vector.substring(li_punto - 1, li_punto).equals("."))) continue;
      ll_numero += Long.parseLong(ls_vector.substring(li_punto - 1, li_punto)) * li_punto;
    }

    long dig_aux2 = residuo(ll_numero, 11L);
    if (dig_aux2 == 10L) dig_aux2 = 0L;
    codBarra = pmSimbol + ls_importe + dig_aux1 + dig_aux2;
    return codBarra;
  }
  static long residuo(long pmDiv, long pmdiv) {
    long coc = pmDiv / pmdiv;
    long res = pmDiv - pmdiv * coc;
    return res;
  }

}
